package com.hellobike.haxing.modules.supplier.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.modules.product.entity.Category;
import com.hellobike.haxing.modules.product.mapper.CategoryMapper;
import com.hellobike.haxing.modules.supplier.entity.TSupplierInvoice;
import com.hellobike.haxing.modules.supplier.mapper.TSupplierInvoiceMapper;
import com.hellobike.haxing.modules.supplier.service.ITSupplierInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 发票表
 * @Author: haxing-boot
 * @Date:   2020-01-16
 * @Version: V1.0
 */
@Service
public class TSupplierInvoiceServiceImpl extends ServiceImpl<TSupplierInvoiceMapper, TSupplierInvoice> implements ITSupplierInvoiceService {

    @Autowired
    private TSupplierInvoiceMapper tSupplierInvoiceMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<TSupplierInvoice> getBySupplierId(QueryWrapper<TSupplierInvoice> queryWrapper) {
        List<TSupplierInvoice> tSupplierInvoices = tSupplierInvoiceMapper.selectList(queryWrapper);
        fillCategoryNames(tSupplierInvoices);
        return tSupplierInvoices;
    }

    //填充分类名称
    private void fillCategoryNames(List<TSupplierInvoice> tSupplierInvoices) {
        if (tSupplierInvoices != null && tSupplierInvoices.size() > 0) {
            Set<Long> categoryIds = new HashSet<>();
            tSupplierInvoices.forEach(tSupplierInvoice -> {
                if(tSupplierInvoice.getFirstCategory() != null && tSupplierInvoice.getSecondCategory() != null && tSupplierInvoice.getThirdCategory() != null) {
                    categoryIds.add(Long.valueOf(tSupplierInvoice.getFirstCategory()));
                    categoryIds.add(Long.valueOf(tSupplierInvoice.getSecondCategory()));
                    categoryIds.add(Long.valueOf(tSupplierInvoice.getThirdCategory()));
                }
            });
            List<Category> categoryList = categoryMapper.selectBatchIds(categoryIds);
            Map<Long, String> categoryNameMap = categoryList.stream().collect(Collectors.toMap(Category::getId, Category::getName));
            tSupplierInvoices.forEach(tSupplierInvoice -> {
                if(tSupplierInvoice.getFirstCategory() != null && tSupplierInvoice.getSecondCategory() != null && tSupplierInvoice.getThirdCategory() != null) {
                    tSupplierInvoice.setFirstCategoryName(categoryNameMap.get(Long.valueOf(tSupplierInvoice.getFirstCategory())));
                    tSupplierInvoice.setSecondCategoryName(categoryNameMap.get(Long.valueOf(tSupplierInvoice.getSecondCategory())));
                    tSupplierInvoice.setThirdCategoryName(categoryNameMap.get(Long.valueOf(tSupplierInvoice.getThirdCategory())));
                }
            });
        }
    }
}
